﻿using SwenAssigment.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class AccountManageDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int vaildID = 0;
                try
                {
                    vaildID = Convert.ToInt32(Request.QueryString["uid"].ToString());
                }
                catch (Exception ex)
                {
                    ClientScriptManager cs = Page.ClientScript;
                    cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Query String Error');window.location.href = 'AccountManagement.aspx';", true);
                }
                if (vaildID != 0)
                {
                    if (JcUser.GetAllUser(1, Request.QueryString["uid"].ToString()).Count == 1)
                    {
                        UserDetail EditUser = (UserDetail)JcUser.GetAllUser(1, Request.QueryString["uid"].ToString())[0];
                        //User ID:Staff ID:User Name:NRIC:Job:Contact Number:
                        Label1.Text = EditUser.User_ID.ToString();
                        Label2.Text = EditUser.Staff_ID.ToString();
                        Label3.Text = EditUser.Username;
                        Label4.Text = EditUser.NRIC;
                        Label5.Text = EditUser.Job;
                        Label6.Text = EditUser.Contact_Number.ToString();

                        if (EditUser.Role == "Normal")
                        {
                            DropDownList1.SelectedIndex = 0;
                        }
                        else if (EditUser.Role == "Management")
                        {
                            DropDownList1.SelectedIndex = 1;
                        }
                        else if (EditUser.Role == "Admin")
                        {
                            DropDownList1.SelectedIndex = 2;
                        }
                        DropDownList2.SelectedIndex = EditUser.LockRate;
                        Panel1.Visible = true;
                    }
                }


            }
        }

        protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;
            if (CheckBox1.Checked == true)
            {
                Match match = Regex.Match(TextBox1.Text, @"^[a-zA-Z0-9]{4,20}$");
                if (match.Success && TextBox2.Text == TextBox1.Text)
                {
                    if (JcUser.UpdateUser(Convert.ToInt32(Label1.Text), 2, DropDownList1.SelectedValue, SecureFunction.hashvalue(TextBox1.Text), DropDownList2.SelectedIndex) == 0)
                    {
                        cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Fail');", true);
                    }
                    else
                    {
                        cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Updated');window.location.href = 'AccountManagement.aspx';", true);
                    }
                }
                else
                {
                    if (match.Success == false)
                    {
                        Label7.Visible = true;
                    }
                    if (TextBox2.Text != TextBox1.Text)
                    {
                        Label8.Visible = true;
                    }
                }
            }
            else
            {
                if (JcUser.UpdateUser(Convert.ToInt32(Label1.Text), 2, DropDownList1.SelectedValue, "", DropDownList2.SelectedIndex) == 0)
                {
                    cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Fail');", true);
                }
                else
                {
                    cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Updated');window.location.href = 'AccountManagement.aspx';", true);
                }
            }
            // cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('login fail');window.location.href = 'http://www.google.com';", true);
        }
    }
}